<?php
if(!function_exists('cool_table_name')) {
    function cool_table_name($_table_name){
        return config('database.connections.mysql.prefix').$_table_name;
    }
}
/**
 * 上传远程附件
 * @param string $_relative_path 系统附件相对路径
 * @param bool $_delete_localhost 是否删除本都文件
 * @param bool $_custom_path 是否自定义本地绝对路径
 * @param string $_storage_type 远程附件类型
 * @return mixed
 * @throws Exception
 */
if(!function_exists('cool_upload_remote')) {
    function cool_upload_remote($_relative_path, $_delete_localhost = true, $_custom_path = false, $_storage_type = '')
    {
        $_remote_path = '';
        if($_custom_path) {
            $_absolute_path = $_relative_path;
            $_temp_remote_path = explode('/', $_relative_path);
            $_remote_path = end($_temp_remote_path);
        } else {
            $_absolute_path = \Illuminate\Support\Facades\Storage::path($_relative_path);
            $_remote_path = $_relative_path;
        }
        if(empty($_storage_type)) {
            $_storage_type = \App\Models\SettingMedia::value('storage');
        }
        $RemoteAttachment = null;
        $_setting = \App\Models\SettingMedia::value($_storage_type);
        switch ($_storage_type) {
            case 'qn':
                $RemoteAttachment = new \Kuyuan\Phpwidget\RemoteAttachment\QN($_setting);
                break;
            case 'al':
                $RemoteAttachment = new \Kuyuan\Phpwidget\RemoteAttachment\AL($_setting);
                break;
            case 'tx':
                unset($_setting['domain']);
                $RemoteAttachment = new \Kuyuan\Phpwidget\RemoteAttachment\TX($_setting);
                break;
        }
        $_result = $RemoteAttachment->push($_absolute_path,$_remote_path);
        if($_result && $_delete_localhost) {
            @unlink($_absolute_path);
        }
        return $_result;
    }
}

/**
 * 删除远程附件
 * @param $_relative_path
 * @param $_storage_type
 * @return bool|mixed|void
 */
if(!function_exists('cool_delete_remote')) {
    function cool_delete_remote($_relative_path,$_storage_type = '')
    {
        if(empty($_storage_type)) {
            $_storage_type = App\Models\SettingMedia::value('storage');
        }
        if($_storage_type == 'default') return true;
        $_setting = \App\Models\SettingMedia::value($_storage_type);
        $RemoteAttachment = null;
        switch ($_storage_type) {
            case 'qn':
                $RemoteAttachment = new \Kuyuan\Phpwidget\RemoteAttachment\QN($_setting);
                break;
            case 'al':
                $RemoteAttachment = new \Kuyuan\Phpwidget\RemoteAttachment\AL($_setting);
                break;
            case 'tx':
                unset($_setting['domain']);
                $RemoteAttachment = new \Kuyuan\Phpwidget\RemoteAttachment\TX($_setting);
                break;
        }
        return $RemoteAttachment->remove($_relative_path);
    }
}

/**
 * 将附件相对链接转为可访问url
 * @param string $_url 文件链接地址
 * @return string
 */
if(!function_exists('cool_media')) {
    function cool_media($_url)
    {
        // 先测试系统配置的远程产品，在测试本地，然后测试其他的
        if(strpos($_url,'http') !== 0) {
              if(is_file(\Illuminate\Support\Facades\Storage::path($_url))) {
                  $_url = config('app.url').\Illuminate\Support\Facades\Storage::url($_url);
              } else {
                  $_storage_type = \App\Models\SettingMedia::value('storage');
                  if($_storage_type != 'default'){
                        $_storage_setting = \App\Models\SettingMedia::value($_storage_type);
                        $_url = $_storage_setting['domain'].'/'.$_url;
                  } else {
                      $_storage_setting = \App\Models\SettingMedia::value('al');
                      $_temp_url = $_storage_setting['domain'].'/'.$_url;
                      if(\Kuyuan\Phpwidget\File::existsRemote($_temp_url)) {
                          return $_temp_url;
                      }
                      $_storage_setting = \App\Models\SettingMedia::value('qn');
                      $_temp_url = $_storage_setting['domain'].'/'.$_url;
                      if(\Kuyuan\Phpwidget\File::existsRemote($_temp_url)) {
                          return $_temp_url;
                      }
                      $_storage_setting = \App\Models\SettingMedia::value('tx');
                      $_temp_url = $_storage_setting['domain'].'/'.$_url;
                      if(\Kuyuan\Phpwidget\File::existsRemote($_temp_url)) {
                          return $_temp_url;
                      }
                  }
              }
        }
        return $_url;
    }
}


/**
 * 敏感关键词替换
 * @param $_content
 * @return string|string[]
 */
if(!function_exists('sensitiveWord')) {
    function sensitiveWord($_content)
    {
        $_sensitive_word = \App\Models\Setting::value('sensitive_word');
        $_sensitive_word = explode(',',$_sensitive_word);
        if($_sensitive_word){
            $_replace = array();
            foreach($_sensitive_word as $_v) {
                $_replace[] = str_repeat("*",mb_strlen($_v));
            }
            $_content = str_replace($_sensitive_word,$_replace,$_content);
        }
        return $_content;
    }
}

/**
 * 判断客户端是不是手机微信浏览器
 */
if(!function_exists('test_wechat')) {
    function test_wechat()
    {
        $ua = $_SERVER['HTTP_USER_AGENT'];
        if(strpos(strtolower($ua), 'micromessenger') === false ||  strpos(strtolower($ua),"windowswechat") === true) {
            return false;
        } else {
            return true;
        }
    }
}

/**
 * 运行swoole抢红包同步数据
 * @param boolean $_daemonize 守护进程
 */
if(!function_exists('cool_red_timer')) {
    function cool_red_timer($_daemonize = false) {
        //创建Server对象，监听 127.0.0.1:9501 端口
        $server = new Swoole\Server('127.0.0.1', 9287);
        $server->set([
            'daemonize' => $_daemonize,
            'worker_num' => 2
        ]);
        //监听连接进入事件
        $server->on('WorkerStart', function (Swoole\Server $server, $workerId) {
            if ($workerId === 0) {
                $server->tick(1000, function () use ($server) {
                    \Illuminate\Support\Facades\Redis::set('cool_red_timer_master_pid',$server->master_pid);
                });
            }
        });
        //监听数据接收事件
        $server->on('Receive', function () {});
        //启动服务器
        $server->start();
    }
}
